from rest_framework import serializers
from .models import UserInfo,Addr

class VipCourseDateSerializer(serializers.Serializer):
  stu_number = serializers.CharField(max_length=20)
  sum_number = serializers.CharField(max_length=20)
  course_score = serializers.CharField(max_length=20)
  price = serializers.CharField(max_length=40)

class UserInfoSerializer(serializers.Serializer):
  name = serializers.CharField(max_length=20)
  password = serializers.CharField(max_length=20)
  email = serializers.EmailField(max_length=40)
  age = serializers.IntegerField(min_value=0,max_value=120)
  id = serializers.IntegerField(read_only=True)
  def create(self, validated_data):
   
    return  UserInfo.objects.create(validated_data)
  def update(self, instance, validated_data):
    instance.name = validated_data['name']
    instance.password = validated_data['password']
    instance.email = validated_data['email']
    instance.age = validated_data['age']
    instance.save()
    return instance
class AddrSerializer(serializers.Serializer):
  id = serializers.IntegerField(read_only=True)
  mobile = serializers.CharField(max_length=11)
  city = serializers.CharField(max_length=10)
  info = serializers.CharField(max_length=120)
  # 返回关联模型对象
  user = serializers.StringRelatedField()

#模型对象的__str__返回的结果
# user= StringRelatedField()
#模型对象返回的结果
# user = UserInfoSerializer()
#返回模型对象的某一个字段
# user = serializers.SlugRelatedField(read_only=True,slug_filed='name')
